package com.jyr.ohs.service.impl;

import com.jyr.ohs.common.FileUtils;
import com.jyr.ohs.common.GeneratorUtils;
import com.jyr.ohs.common.UnifiedJsonReturn;
import com.jyr.ohs.entity.*;
import com.jyr.ohs.mapper.*;
import com.jyr.ohs.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhongJing </p>
 * @Description </p>
 * @date 2021/4/15 11:59 上午 </p>
 */
@Slf4j
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    HomeworkMapper homeworkMapper;

    @Autowired
    SubmitWorkMapper submitWorkMapper;

    @Override
    public UnifiedJsonReturn<Teacher> login(Teacher teacher) {
        // 根据id查询教师信息
        Teacher selectTeacher = teacherMapper.seleyById(teacher.getId());

        // 如果不存在对应教师信息，返回错误信息
        if (selectTeacher == null) {
            return new UnifiedJsonReturn<Teacher>().failed().message("用户名不存在，请检查您的用户名并重新输入！");
        }

        // 教师信息存在，判断密码是否正确
        if (selectTeacher.getPassword().equals(teacher.getPassword())) {
            // 密码正确返回登陆成功，并且返回教师信息
            return new UnifiedJsonReturn<Teacher>().success().message("登陆成功！").data(selectTeacher);
        }

        // 密码错误返回错误信息
        return new UnifiedJsonReturn<Teacher>().failed().message("密码输入错误，请检查您的密码并重新输入！");
    }

    @Override
    public boolean addList(Integer teacherNum, String password) {

        // 创建教师集合
        List<Teacher> teachers = new ArrayList<>();

        // 给集合循环添加教师对象
        for (int i = 0; i < teacherNum; i++) {
            // 创建teacher对象
            Teacher teacher = new Teacher();
            teacher.setName(GeneratorUtils.randomName());
            teacher.setPassword(password);

            // 将对象添加到集合中
            teachers.add(teacher);
            // 记录日志
            log.info("教师信息" + i + "：" + teacher);
        }
        log.info("教师信息" + teachers.size() + "个已全部生成！");

        // 持久化
        return teacherMapper.addList(teachers);

    }

    @Override
    public List<Course> getAllCourseById(Integer id) {
        return courseMapper.getCourseListByTid(id);
    }

    @Override
    public boolean publishHomework(Issue issue, MultipartFile file) {
        // 判断文件是否为空
        if (file == null) {
            return false;
        }

        try {
            // 获取上传的文件的后缀名
            String filename = file.getOriginalFilename();
            int index = filename.lastIndexOf(".");
            String suffixFilename = filename.substring(index);

            // 设置目录
            String path = new File("/Users/zhangfan").getCanonicalPath();

            // 拼接路径，规则：项目路径/resources/homework/教师id/课程id/作业名称+上传文件的后缀名
            String filePath = path + "/homework/teacher/" + issue.getCId() + "/" + issue.getHomeworkName() + suffixFilename;

            // 文件夹路径
            String dirPath = path + "/homework/teacher/" + issue.getCId();

            // 写入文件
            String writePath = FileUtils.write(file, filePath, dirPath);

            // 保存作业信息
            issue.setHomeworkPath(writePath);
            Integer id = issueMapper.add(issue);
            issue.setId(id);

            // 添加信息到作业表
            // 查询该课程一共有多少个学生
            Integer stuCount = courseMapper.countById(issue.getCId());
            // 创建作业表
            Homework homework = new Homework();
            // 设置属性
            homework.setId(issue.getId());
            homework.setName(issue.getHomeworkName());
            homework.setCId(issue.getCId());
            homework.setSubmitCount(0);
            homework.setAllCount(stuCount);
            homework.setExpirationTime(issue.getExpirationTime());
            // 保存到数据库
            return homeworkMapper.add(homework);
        } catch (IOException e) {
            log.warn("获取路径出错，错误类型：" + e.getClass());
            log.warn("错误信息：" + e.getMessage());
            return false;
        }
    }

    @Override
    public List<Homework> getHomeworkListByTid(Integer tid) {
        return homeworkMapper.getHomeworkListByTid(tid);
    }

    @Override
    public List<SubmitWork> getSubmitWorkListByCid(Integer cid) {
        return submitWorkMapper.getListByCid(cid);
    }
}
